package com.erbao.disruptor;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

/**
 * @author CaoBaoYin
 * @since 22/3/10 10:25
 */
public class Main04ProducerType {

    public static void main(String[] args) {
        // 创建工厂
        LongEventFactory longEventFactory = new LongEventFactory();
        // 队列大小
        int ringBufferSize = 1024;
        // 创建disruptor。线程工厂里创建线程，在这个线程里调用消费者，不同的消费者会在不同的线程
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(longEventFactory, ringBufferSize, Executors.defaultThreadFactory(), ProducerType.SINGLE, new BlockingWaitStrategy());
        // 指定消费者
        disruptor.handleEventsWith(new LongEventHandler(), new LongEventHandler());

        // 启动
        disruptor.start();

        // 生产event
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        // 不带参数
        EventTranslator<LongEvent> translator = new EventTranslator<LongEvent>() {
            @Override
            public void translateTo(LongEvent event, long sequence) {
                event.setValue(1000L);
            }
        };
        ringBuffer.publishEvent(translator);

        EventTranslatorOneArg<LongEvent, Long> translatorOneArg = new EventTranslatorOneArg<LongEvent, Long>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Long arg0) {
                event.setValue(arg0);
            }
        };
        ringBuffer.publishEvent(translatorOneArg, 1002L);

        EventTranslatorTwoArg<LongEvent, Long, Long> translatorTwoArg = new EventTranslatorTwoArg<LongEvent, Long, Long>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Long arg0, Long arg1) {
                event.setValue(arg0+arg1);
            }
        };
        ringBuffer.publishEvent(translatorTwoArg, 1002L, 1002L);

        EventTranslatorThreeArg<LongEvent, Long, Long, Long> translatorThreeArg = new EventTranslatorThreeArg<LongEvent, Long, Long, Long>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Long arg0, Long arg1, Long arg2) {
                event.setValue(arg0+arg1+arg2);
            }
        };
        ringBuffer.publishEvent(translatorThreeArg, 1003L, 1003L, 1003L);

        EventTranslatorVararg<LongEvent> translatorVararg = new EventTranslatorVararg<LongEvent>() {
            @Override
            public void translateTo(LongEvent event, long sequence, Object... args) {
                long result = 0L;

                for (Object arg : args) {
                    long l = 0L;
                    if (arg instanceof Long) {
                        l = (long) arg;
                    }
                    result += l;
                }

                event.setValue(result);
            }
        };
        ringBuffer.publishEvent(translatorVararg, 1006L, 1006L, 1006L, 1006L, 1006L, 1006L);

        /*try {
            disruptor.shutdown(5L, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        }*/

        /*try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

}
